package com.gzc.just.play.last.war.common.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Message service
 * Handles chat, system messages, etc.
 */
@Service
public class MessageService {
    
    private static final Logger log = LoggerFactory.getLogger(MessageService.class);
    
    // Player online status
    private final ConcurrentHashMap<Long, Boolean> onlinePlayers = new ConcurrentHashMap<>();
    
    // Player block list
    private final ConcurrentHashMap<Long, Map<Long, String>> playerBlocks = new ConcurrentHashMap<>();
    
    /**
     * 发送系统消息
     * @param playerId 玩家ID
     * @param message 消息内容
     * @param messageType 消息类型
     */
    public void sendSystemMessage(Long playerId, String message, String messageType) {
        // 实际实现中应该通过Netty发送消息到客户端
        log.info("Sent system message to player {}: {} ({})", playerId, message, messageType);
        
        // 模拟发送成功
        return;
    }
    
    /**
     * 发送聊天消息
     * @param senderId 发送者ID
     * @param receiverId 接收者ID（可选）
     * @param message 消息内容
     * @param chatType 聊天类型
     */
    public void sendChatMessage(Long senderId, Long receiverId, String message, String chatType) {
        // 检查玩家是否在线
        if (!isPlayerOnline(senderId)) {
            log.warn("Player {} is offline, cannot send chat message", senderId);
            return;
        }
        
        // 检查是否被屏蔽
        if (receiverId != null && isPlayerBlocked(senderId, receiverId)) {
            log.debug("Player {} has blocked player {}, chat message filtered", receiverId, senderId);
            return;
        }
        
        // 发送消息
        log.info("Chat message from {} to {} ({}): {}", 
                 senderId, receiverId != null ? receiverId : "ALL", chatType, message);
        
        // 实际实现中应该通过Netty发送消息
        if (receiverId != null) {
            // 私聊消息
            sendPrivateChatMessage(senderId, receiverId, message, chatType);
        } else {
            // 公聊/频道消息
            sendPublicChatMessage(senderId, message, chatType);
        }
    }
    
    /**
     * 发送私聊消息
     * @param senderId 发送者ID
     * @param receiverId 接收者ID
     * @param message 消息内容
     * @param chatType 聊天类型
     */
    private void sendPrivateChatMessage(Long senderId, Long receiverId, String message, String chatType) {
        // 检查接收者是否在线
        if (!isPlayerOnline(receiverId)) {
            sendSystemMessage(senderId, "玩家离线，消息无法送达", "ERROR");
            return;
        }
        
        // 实际实现中应该通过Netty发送消息
        log.debug("Private chat from {} to {}: {}", senderId, receiverId, message);
    }
    
    /**
     * 发送公聊消息
     * @param senderId 发送者ID
     * @param message 消息内容
     * @param chatType 聊天类型
     */
    private void sendPublicChatMessage(Long senderId, String message, String chatType) {
        // 实际实现中应该通过Netty发送消息到所有在线玩家
        log.debug("Public chat from {} ({}): {}", senderId, chatType, message);
    }
    
    /**
     * 设置玩家在线状态
     * @param playerId 玩家ID
     * @param online 是否在线
     */
    public void setPlayerOnline(Long playerId, boolean online) {
        if (online) {
            onlinePlayers.put(playerId, true);
            log.debug("Player {} is now online", playerId);
        } else {
            onlinePlayers.remove(playerId);
            log.debug("Player {} is now offline", playerId);
        }
    }
    
    /**
     * 检查玩家是否在线
     * @param playerId 玩家ID
     * @return 是否在线
     */
    public boolean isPlayerOnline(Long playerId) {
        return onlinePlayers.containsKey(playerId) && onlinePlayers.get(playerId);
    }
    
    /**
     * 屏蔽玩家
     * @param playerId 玩家ID
     * @param blockedId 被屏蔽的玩家ID
     * @param reason 屏蔽原因
     */
    public void blockPlayer(Long playerId, Long blockedId, String reason) {
        Map<Long, String> blockList = playerBlocks.computeIfAbsent(playerId, id -> new ConcurrentHashMap<>());
        blockList.put(blockedId, reason != null ? reason : "已屏蔽");
        log.info("Player {} blocked player {} for reason: {}", playerId, blockedId, reason);
    }
    
    /**
     * 取消屏蔽玩家
     * @param playerId 玩家ID
     * @param blockedId 被屏蔽的玩家ID
     */
    public void unblockPlayer(Long playerId, Long blockedId) {
        Map<Long, String> blockList = playerBlocks.get(playerId);
        if (blockList != null) {
            String reason = blockList.remove(blockedId);
            log.info("Player {} unblocked player {} (was blocked for: {})", 
                     playerId, blockedId, reason);
        }
    }
    
    /**
     * 检查玩家是否被屏蔽
     * @param playerId 玩家ID
     * @param blockedId 被检查的玩家ID
     * @return 是否被屏蔽
     */
    public boolean isPlayerBlocked(Long playerId, Long blockedId) {
        Map<Long, String> blockList = playerBlocks.get(playerId);
        return blockList != null && blockList.containsKey(blockedId);
    }
    
    /**
     * 获取在线玩家数量
     * @return 在线玩家数量
     */
    public int getOnlinePlayerCount() {
        return onlinePlayers.size();
    }
}